package top.jlzhou.test_data.commons;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import top.jlzhou.test_data.city.CityService;
import top.jlzhou.T;

/**
 * 随机获取用户信息
 * @author Administrator
 *
 */
public class TestUser {
	
	private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	private TestUser(){}
	
	public static TestUser random(){
		return random(new Date(),-365*90);
	}
	
	/**
	 * 
	 * @param birthday 生日种子
	 * @param day	生日随机天数 负数表示在种子之前
	 * @return
	 */
	public static TestUser random(Date birthday,int day){
		Random r = new Random();
		TestUser user = new TestUser();
		user.name = RandomUser.getChineseName();
		user.sex = RandomUser.getSex();
		user.qq = RandomUser.getQq();
		if(r.nextInt(100)<95){
			user.birthday = randomDate(birthday, day);
		}
		if(r.nextInt(100)<95){
			if(user.birthday==null){
				user.cardId = IdCardGenerator.generate();
			}else{
				user.cardId = IdCardGenerator.generate(user.birthday);
			}
		}
		if(r.nextInt(100)<85){
			user.tel = RandomUser.getTel();
		}
		if(r.nextInt(100)<75){
			user.email = RandomUser.getEmail(3, 12);
		}
		if(r.nextInt(100)<95){
			try{
				user.address=CityService.getService().randomAddress()+RandomUser.getRoad();
			}catch(Exception ex){
				
			}
		}
		
		user.nameSpell = "";
		user.nameSpellFull = "";
		char[] cs = user.name.toCharArray();
		for(char c : cs){
			String py = T.getInstance().spell.trans2PinYin(""+c);
			py = py.substring(0, 1).toUpperCase()+py.substring(1).toLowerCase();
			user.nameSpell += py.subSequence(0, 1);
			user.nameSpellFull += py;
		}
		
		return user;
	}
	
	private static Date randomDate(Date date,int day){
		try{
			date = format.parse(format.format(date));
		}catch(Exception ex){}
		
		Random random = new Random();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		if(day>0){
			c.add(Calendar.DATE, random.nextInt(day));
		}else{
			c.add(Calendar.DATE, -random.nextInt(-day));
		}
		return c.getTime();
		
	}
	
	/**
	 * 姓名
	 */
	private String name;
	
	/**
	 * 姓名拼音首字母
	 */
	private String nameSpell;
	
	/**
	 * 姓名全
	 */
	private String nameSpellFull;
	
	private String qq;
	
	
	/**
	 * 性别
	 */
	private Boolean sex;
	
	/**
	 * 生日
	 */
	private Date birthday;
	/**
	 * 身份证号码
	 */
	private String cardId;
	
	/**
	 * 电话
	 */
	private String tel;

	/**
	 * 邮箱
	 */
	private String email;
	
	/**
	 * 地址
	 */
	private String address;

	
	
	public String getQq() {
		return qq;
	}

	public String getName() {
		return name;
	}

	public String getNameSpell() {
		return nameSpell;
	}

	public String getNameSpellFull() {
		return nameSpellFull;
	}

	public Boolean getSex() {
		return sex;
	}

	public String getCardId() {
		return cardId;
	}

	public String getTel() {
		return tel;
	}

	public String getEmail() {
		return email;
	}

	public String getAddress() {
		return address;
	}
	
	

	public Date getBirthday() {
		return birthday;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("TestUser [name=");
		builder.append(name);
		builder.append(", nameSpell=");
		builder.append(nameSpell);
		builder.append(", nameSpellFull=");
		builder.append(nameSpellFull);
		builder.append(", sex=");
		builder.append(sex);
		builder.append(", birthday=");
		if(birthday!=null){
			builder.append(format.format(birthday));
		}else{
			builder.append("null");
		}
		builder.append(", cardId=");
		builder.append(cardId);
		builder.append(", tel=");
		builder.append(tel);
		builder.append(", email=");
		builder.append(email);
		builder.append(", address=");
		builder.append(address);
		builder.append("]");
		return builder.toString();
	}

	
	
}
